package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 分类管理
 */

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 接口一：新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
//输入为空
        if(category==null||StringUtils.isEmpty(category.getName())||category.getSort()==null)
            return R.error("输入不得为空");
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,category.getName());
        Category cate = categoryService.getOne(queryWrapper);
//        1、输入的分类名称不存在
        if(cate!=null)
            return R.error("输入的分类名称已存在");
//        2、输入已存在的分类名称

        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 分类分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
        log.info("page:{},pageSize:{}",page,pageSize);
        //分页构造器
        Page pageInfo=new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> qw=new LambdaQueryWrapper<>();

        categoryService.page(pageInfo,qw);
        return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> deleteById(Long id){
        //当分类关联了菜品或者套餐时，此分类不允许删除。

        log.info("删除分类的id为：{}",id);
     //   categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("删除成功");

    }

    /**
     * 根据Id修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info(category.toString());
        long id = Thread.currentThread().getId();

        log.info("线程id为："+id);
        categoryService.updateById(category);
        return R.success("分类修改完成");
    }

    /**
     *
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> l = categoryService.list(queryWrapper);
        return R.success(l);
    }
}
